package com.example.campusforumapi.service.impl;

import com.example.campusforumapi.dao.CategoryMapper;
import com.example.campusforumapi.dao.DepartmentMapper;
import com.example.campusforumapi.entity.Category;
import com.example.campusforumapi.entity.Department;
import com.example.campusforumapi.service.CategoryService;
import com.example.campusforumapi.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public PageResult getCagegoryPage(Integer page, Integer pageSize) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Category> CategoryList = categoryMapper.list();
        Page<Category> p = (Page<Category>) CategoryList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public int delCategory(List<Integer> ids) {
        return categoryMapper.delCategory(ids);
    }

    @Override
    public int findsameCategoryName(String categoryName) {
        return categoryMapper.findsameCategoryName(categoryName);
    }

    @Override
    public int findsameCategoryId(Integer categoryId) {
        return categoryMapper.findsameCategoryId(categoryId);
    }

    @Override
    public int insertCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        return categoryMapper.insertCategory(category);
    }

    @Override
    public int updCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());//更新修改时间
        return categoryMapper.updCategory(category);
    }
}
